home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCommon / Include / FWThrdGd.h < prev   
Encoding:
Text File  |  1995-11-08  |  5.0 KB  |  156 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWThrdGd.h
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWTHRDGD_H
  11. #define FWTHRDGD_H
  12.  
  13. #ifndef FWSTDDEF_H
  14. #include "FWStdDef.h"
  15. #endif
  16.  
  17. #ifdef FW_BUILD_MAC
  18. #include <Threads.h>
  19. #endif
  20.  
  21. #if FW_LIB_EXPORT_PRAGMAS
  22. #pragma lib_export on
  23. #endif
  24.  
  25.  
  26. //==============================================================================
  27. // Theory of Operation
  28. //==============================================================================
  29.  
  30. // FW_CThreadSafe is a class exporting static methods which parts 
  31. // must call if they are using threads.  The methods should be called 
  32. // within a critical region; they are not thread-safe.
  33.  
  34. // FW_CThreadGuard is an abstract base class associating threads with operations 
  35. // which must be done to assure thread-safe execution.  Instances of FW_CThreadGuard 
  36. // are always statically allocated and provide protection for global variables.
  37.  
  38. //==============================================================================
  39. // Constants
  40. //==============================================================================
  41.  
  42. //==============================================================================
  43. // Scalar Types
  44. //==============================================================================
  45.  
  46. #if defined    FW_BUILD_MAC
  47. typedef    ThreadID            FW_ThreadID;
  48. #elif defined FW_BUILD_WIN
  49. typedef    unsigned long        FW_ThreadID;
  50. #endif
  51.  
  52. //=====================================================================================
  53. // Classes defined in this interface
  54. //=====================================================================================
  55.  
  56. class FW_CThreadSafe;    
  57. class FW_CThreadGuard;    
  58.  
  59. //=====================================================================================
  60. // Classes used by this interface
  61. //=====================================================================================
  62.  
  63. //=====================================================================================
  64. // Class FW_CThreadSafe
  65. //=====================================================================================
  66.  
  67. class FW_CLASS_ATTR FW_CThreadSafe {
  68. public:
  69.     static void NoteCreation(FW_ThreadID newlyCreatedThread);
  70.     static void NoteTermination(FW_ThreadID threadBeingKilled);
  71.         // Parts call NoteCreation when creating a new thread and 
  72.         // NoteTermination when a thread is being destroyed.
  73.  
  74. #ifdef    FW_BUILD_MAC
  75.     static void NoteSwitch(FW_ThreadID aThread, FW_Boolean switchingIn);
  76.         // A thread-using part must call this method when a thread 
  77.         // switch occurs.  The boolean is TRUE when the thread is being
  78.         // switched in, FALSE when the thread is being switched out.
  79. #endif
  80.  
  81. private:
  82.     friend class FW_CThreadGuard;
  83.     static void RegisterTerminationGuard(FW_CThreadGuard *aThreadGuard);
  84.     static void DeRegisterTerminationGuard(FW_CThreadGuard *aThreadGuard);
  85.  
  86.     static void RegisterSwitchGuard(FW_CThreadGuard *aThreadGuard);
  87.     static void DeRegisterSwitchGuard(FW_CThreadGuard *aThreadGuard);
  88.  
  89. private:
  90.     static FW_CThreadGuard *fgSwitchGuards;
  91.     static FW_CThreadGuard *fgTerminationGuards;
  92. };
  93.  
  94.  
  95. //=====================================================================================
  96. // Class FW_CThreadGuard
  97. //=====================================================================================
  98.  
  99. class FW_CLASS_ATTR FW_CThreadGuard {
  100. protected:
  101.     enum {
  102.         kNoThreadEvent            = 0x00,
  103.         kSwitchInThreadEvent    = 0x01,
  104.         kSwitchOutThreadEvent   = 0x02,
  105.  
  106.         kAllThreadEvents = kSwitchInThreadEvent | kSwitchOutThreadEvent
  107.     };
  108.  
  109.     FW_CThreadGuard(unsigned long threadEventMask = kAllThreadEvents);
  110.     virtual ~FW_CThreadGuard();
  111.  
  112.     virtual void Created(FW_ThreadID newlyCreatedThread) = 0;
  113.     virtual void Terminating(FW_ThreadID threadBeingKilled) = 0;
  114.         // These methods are called when threads are created and
  115.         // terminated.
  116.  
  117. #ifdef    FW_BUILD_MAC
  118.     virtual void Switch(FW_ThreadID aThread, FW_Boolean switchingIn);
  119.         // Called when a thread switch takes place.  This is an optional
  120.         // notification: if the constructor did not have kSwitchThreadEvent
  121.         // in its bitmask the guard will not be notified of thread switches.
  122.         // switchingIn is TRUE if aThread will be next to run, FALSE if it 
  123.         // being switched out.
  124. #endif
  125.  
  126.     void AddThreadInfo(FW_ThreadID aThreadID, void *threadInfo);
  127.     void RemoveThreadInfo(FW_ThreadID aThreadID);
  128.     void *GetThreadInfo(FW_ThreadID aThreadID);
  129.         // These methods allow instances of derived classes to associate 
  130.         // a chunk of data with a thread.  AddThreadInfo() should be called 
  131.         // from "Created()", RemoveThreadInfo() should be called from 
  132.         // "Terminating()" and GetThreadInfo() from "Switching()".
  133.  
  134.     unsigned long GetMask() const;
  135.  
  136. private:
  137.     friend class FW_CThreadSafe;
  138.     FW_CThreadGuard *fNext;
  139.     unsigned long fMask;
  140.  
  141. private:
  142.     struct FW_SThreadItem {
  143.         FW_ThreadID     fThreadID;
  144.         void           *fThreadInfo;
  145.         FW_SThreadItem *fNext;
  146.     };
  147.     FW_SThreadItem *fActiveThreads;
  148. };
  149.  
  150.  
  151. #if FW_LIB_EXPORT_PRAGMAS
  152. #pragma lib_export off
  153. #endif
  154.  
  155. #endif
  156.